﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime;
using System.Data;
using System.IO.Ports;

namespace C_DOS
{
    class Program
    {
        private static object c;
        void COMMANDERROR()
        {
            Console.WriteLine("ERROR: No such command found!!!");
        }
        static void Main(string[] args)
        {
        boot://begin "boot" sequence:
            string welcome = "Welcome to C-DOS";
            string bootstart = "Begining the boot process...";
            string bootload = "BootLoader executing.....";
            string loadmainsys = "Loading Main System files.....";
            for (int i = 0; i < 50; i++)
            {
                string load = "Booting.....";
                int T;
                for (T = 0; T < 100; T++)
                {
                    T++;
                    Console.Clear();
                    Console.WriteLine(welcome);
                    Console.WriteLine(bootload);
                    Console.WriteLine(bootstart);
                    Console.WriteLine(loadmainsys);
                    Console.WriteLine(load + T.ToString());
                    if (T == 100)
                    {
                        break;
                    }
                }
                T = 0;
                Console.Clear();
                if (i == 50)
                {
                    break;
                }
            }
            string HELP = "help";
            string DIRECTORY = "dir";
            string EDIT = "edit";
            string VER = "ver";
            string RESTART = "restart";
            string SHUTOFF = "shutdown";
            string CLEAR = "clr";
            string RUN = "run";
            string MATH = "math";
            string LIST = "ls";
            string BCK = "back";
            string EXT = "exit";
            Console.WriteLine("Done.");
            Console.Clear();
            Console.WriteLine("Welcome to C-DOS!");
        begin://main loop start
            string DateAndTime = DateTime.Today.Date.ToShortDateString();
            Console.WriteLine(DateAndTime);
            Console.Write("\\CDOS@root:");
            string command = Console.ReadLine();
            try
            {
                if (command == HELP)//help menu command
                {
                    Console.WriteLine("==============Help==================");
                    Console.WriteLine("| help = help menu(obviously)      |");
                    Console.WriteLine("| dir = current working directory  |");
                    Console.WriteLine("| edit = C-DOS TextWriter          |");
                    Console.WriteLine("| math = Math program(calculator)  |");
                    Console.WriteLine("| ver = C-DOS Version              |");
                    Console.WriteLine("| restart = restart C-DOS          |");
                    Console.WriteLine("| shutdown = shutdown C-DOS        |");
                    Console.WriteLine("| clr = clear the screen           |");
                    Console.WriteLine("| run = run an external program    |");
                    Console.WriteLine("| NOTE: must be located in current |");
                    Console.WriteLine("| working directory                |");
                    Console.WriteLine("====================================");
                    goto begin;
                }
                else
                    if (command == DIRECTORY)//dir command
                    {
                        string dir = Directory.GetCurrentDirectory();
                        Console.WriteLine(dir);
                        goto begin;
                    }
                    else
                        if (command == EDIT)//edit command
                        {
                            Console.WriteLine("Welcome to DOSWriter");
                            Console.WriteLine("Version 1.0.0");
                        dosedit://doswriter main loop
                            Console.Write("\\DOSWriter:");
                            string editcom = Console.ReadLine();
                            if (editcom == "help")//doswriter help command
                            {
                                Console.WriteLine("==========DOSWriterHelp==========");
                                Console.WriteLine("| help = DOSWriter Help Menu    |");
                                Console.WriteLine("| new = new text file           |");
                                Console.WriteLine("| open = open a text file       |");
                                Console.WriteLine("| exit = exit the writer        |");
                                Console.WriteLine("| clr = Clear the screen        |");
                                Console.WriteLine("|                               |");
                                Console.WriteLine("| Note: Files written in this   |");
                                Console.WriteLine("| writer will be saved in the   |");
                                Console.WriteLine("| '.dat' format.                |");
                                Console.WriteLine("=================================");
                                goto dosedit;
                            }
                            else
                                if (editcom == "new")//doswriter create a new file command
                                {
                                    Console.Write(">");
                                    string docslotsource = Console.ReadLine();
                                    long docslot = Convert.ToInt32(docslotsource);
                                beginsave://the save file "dialog" mini-loop
                                    Console.WriteLine("Do you wish to save this Document?");
                                    Console.Write("[y] or [n]");
                                    string savedialog = Console.ReadLine();
                                    if (savedialog == "y")//say yes to saving the document you just typed
                                    {
                                        Console.WriteLine("Note: Document will be saved in current working directory.");
                                        Console.Write("Name for Document:");//give it a name
                                        string documentname = Console.ReadLine();
                                        string documentsavedirectory = Directory.GetCurrentDirectory();
                                        BinaryWriter documentwriter = new BinaryWriter(File.Create(documentsavedirectory + documentname));//create the document with the name provided in the working directory
                                        documentwriter.Write(docslot);//write the contents typed
                                        goto dosedit;
                                    }
                                    else
                                        if (savedialog == "n")//say no to save, get rid of the hard work of typing
                                        {
                                            Console.WriteLine("Ok, document not saved.");
                                            goto dosedit;
                                        }
                                        else
                                        {
                                            Console.WriteLine("ERROR: Not a correct answer.");//oh no, an error
                                            goto beginsave;
                                        }
                                }
                                else
                                    if (editcom == "open")//lets open a previously made file
                                    {
                                    OpenDocLoop:
                                        Console.Write("Enter the Directory:");//enter the directory of the file
                                        string docdirectory = Console.ReadLine();
                                        if (docdirectory == "exit")
                                        {
                                            Console.WriteLine("Document not opened");
                                            goto dosedit;
                                        }
                                        else
                                            try
                                            {
                                                BinaryReader documentreader = new BinaryReader(File.Open(docdirectory, FileMode.Open));
                                                string thedocument = documentreader.ReadString();//open the file and read the contents
                                                Console.WriteLine(thedocument);
                                            }
                                            catch
                                            {
                                                Console.WriteLine("ERROR: Directory not found!!!");
                                                goto OpenDocLoop;
                                            }
                                        goto dosedit;
                                    }
                                    else
                                        if (editcom == CLEAR)//clear the screen(for the doswriter loop)
                                        {
                                            Console.Clear();
                                            goto dosedit;
                                        }
                                        else
                                            if (editcom == "exit")//exit the doswriter "program"
                                            {
                                                goto begin;
                                            }
                                            else
                                                if (editcom == "")//if you like to type random stuff, this helps prohibits errors
                                                {
                                                    goto dosedit;
                                                }
                        }
                        else
                            if (command == VER)//dos version command
                            {
                                string datecreated   = ("Detailed version:        1.1.2.5242013");
                                string dosversion    = ("C-DOS Version:           1.1.2");
                                string kernelversion = ("C-DOS Kernel version:    2.1");
                                string bootloaderver = ("C-DOS bootloader version 2.1");
                                string doswriterver  = ("DOS Writer version:      2.3");
                                string helpver       = ("Help menu version:       1.5");
                                string directoryver  = ("FileDirectory version:   1.0");
                                string mathver       = ("Math version:            1.0");
                                string runprogver    = ("Run version:             2.0");
                                Console.WriteLine("");
                                Console.WriteLine("=======================================");
                                Console.WriteLine("Copyright c2013 by: Taranoshi          ");
                                Console.WriteLine("Licensed under the GNU public license  ");
                                Console.WriteLine("");
                                Console.WriteLine(dosversion);
                                Console.WriteLine(datecreated);
                                Console.WriteLine(kernelversion);
                                Console.WriteLine(bootloaderver);
                                Console.WriteLine("");
                                Console.WriteLine("Program Versions:--------------------->");
                                Console.WriteLine("");
                                Console.WriteLine(helpver);
                                Console.WriteLine(doswriterver);
                                Console.WriteLine(directoryver);
                                Console.WriteLine(mathver);
                                Console.WriteLine(runprogver);
                                Console.WriteLine("=======================================");
                                Console.WriteLine("");
                                goto begin;
                            }
                            else
                                if (command == RESTART)//restart the virtual dos
                                {
                                    Console.WriteLine("Restarting.....");
                                    Console.WriteLine("Ending Processes.....");
                                    Console.WriteLine("Rebooting");
                                    Console.Beep();
                                    goto boot;
                                }
                                else
                                    if (command == SHUTOFF)//turn off the program
                                    {
                                        Console.WriteLine("Shutting down.....");
                                        Console.WriteLine("Ending Processes.....");
                                        Console.WriteLine("Closing the command interface.....");
                                        Console.Beep();
                                    }
                                    else
                                        if (command == CLEAR)//clear the screen(main loop)
                                        {
                                            Console.WriteLine("Clearing Screen");
                                            Console.Clear();
                                            goto begin;
                                        }
                                        else
                                            if (command == RUN)//run an external file
                                            {
                                                Console.Write("Enter Program Name(must be in current working directory): ");
                                                string runfile = Console.ReadLine();
                                                System.Diagnostics.Process p = new System.Diagnostics.Process();
                                                //your exe here 
                                                string directoryc = Directory.GetCurrentDirectory();
                                                p.StartInfo.WorkingDirectory = directoryc;
                                                p.StartInfo.FileName = runfile;
                                                p.StartInfo.Arguments = "args";
                                                p.StartInfo.UseShellExecute = false;
                                                p.StartInfo.RedirectStandardOutput = true;
                                                //start the exe
                                                p.Start();
                                                //write something 
                                                p.StandardInput.Write("Started the program!!!");
                                                //read the output (!it's a blocking call)
                                                string output = p.StandardOutput.ReadToEnd();
                                                Console.WriteLine(output);
                                                goto begin;
                                            }
                                            else
                                                if (command == MATH)
                                                {
                                                    Console.WriteLine("Welcome to the 'Math' program");
                                                MathLoop:
                                                    Console.Write("\\Math:");
                                                    string mathcommand = Console.ReadLine();
                                                    if (mathcommand == "help")
                                                    {
                                                        Console.WriteLine("============Math Help Menu============");
                                                        Console.WriteLine("| help    = Math help menu           |");
                                                        Console.WriteLine("| add     = add two numbers together |");
                                                        Console.WriteLine("| subtrak = subtract two numbers     |");
                                                        Console.WriteLine("| multi   = multiply two numbers     |");
                                                        Console.WriteLine("| divide  = divide two numbers       |");
                                                        Console.WriteLine("| exit    = exit the Math program    |");
                                                        Console.WriteLine("| clr     = clear the screen         |");
                                                        Console.WriteLine("| NOTE: not able to clear screen     |");
                                                        Console.WriteLine("| while in a'mode'(add, subtrak, etc)|");
                                                        Console.WriteLine("======================================");
                                                        goto MathLoop;
                                                    }
                                                    else
                                                        if (mathcommand == "add")
                                                        {
                                                        addloop:
                                                            try
                                                            {
                                                                Console.Write("Please enter the first number: ");
                                                                string addnumst = Console.ReadLine();
                                                                int addnum1 = Convert.ToInt32(addnumst);
                                                                Console.Write("Please enter the second number: ");
                                                                string addnum2st = Console.ReadLine();
                                                                int addnum2 = Convert.ToInt32(addnum2st);
                                                                int answer = addnum1 + addnum2;
                                                                Console.WriteLine("The answer is: " + answer);
                                                            }
                                                            catch
                                                            {
                                                                Console.WriteLine("ERROR: unable to process");
                                                                goto addloop;
                                                            }
                                                            Console.Write("Do you wish to continue? (yes/no): ");
                                                            string mathexit = Console.ReadLine();
                                                            if (mathexit == "yes")
                                                            {
                                                                goto addloop;
                                                            }
                                                            else
                                                            {
                                                                goto MathLoop;
                                                            }
                                                        }
                                                        else
                                                            if (mathcommand == "subtrak")
                                                            {
                                                            subtrakloop:
                                                                try
                                                                {
                                                                    Console.Write("Please enter first number: ");
                                                                    string subtraknumst = Console.ReadLine();
                                                                    int subtraknum1 = Convert.ToInt32(subtraknumst);
                                                                    Console.Write("Please enter the second number: ");
                                                                    string subtraknumst2 = Console.ReadLine();
                                                                    int subtraknum2 = Convert.ToInt32(subtraknumst2);
                                                                    int answer = subtraknum1 - subtraknum2;
                                                                    Console.WriteLine("The answer is: " + answer);
                                                                }
                                                                catch
                                                                {
                                                                    Console.WriteLine("ERROR: unable to process.");
                                                                    goto subtrakloop;
                                                                }
                                                                Console.Write("Do you wish to continue? (yes/no): ");
                                                                string mathexit = Console.ReadLine();
                                                                if (mathexit == "yes")
                                                                {
                                                                    goto subtrakloop;
                                                                }
                                                                else
                                                                {
                                                                    goto MathLoop;
                                                                }
                                                            }
                                                            else
                                                                if (mathcommand == "multi")
                                                                {
                                                                multiloop:
                                                                    try
                                                                    {
                                                                        Console.Write("PLease enter first number: ");
                                                                        string multinum1st = Console.ReadLine();
                                                                        int multinum1 = Convert.ToInt32(multinum1st);
                                                                        Console.Write("Please enter second number: ");
                                                                        string multinum2st = Console.ReadLine();
                                                                        int multinum2 = Convert.ToInt32(multinum2st);
                                                                        int answer = multinum1 * multinum2;
                                                                        Console.WriteLine("The answer is: " + answer);
                                                                    }
                                                                    catch
                                                                    {
                                                                        Console.WriteLine("ERROR: unable to process.");
                                                                        goto multiloop;
                                                                    }
                                                                    Console.Write("Do you wish to continue? (yes/no): ");
                                                                    string mathexit = Console.ReadLine();
                                                                    if (mathexit == "yes")
                                                                    {
                                                                        goto multiloop;
                                                                    }
                                                                    else
                                                                    {
                                                                        goto MathLoop;
                                                                    }
                                                                }
                                                                else
                                                                    if (mathcommand == "divide")
                                                                    {
                                                                    divideloop:
                                                                        try
                                                                        {
                                                                            Console.Write("PLease enter first number: ");
                                                                            string multinum1st = Console.ReadLine();
                                                                            int multinum1 = Convert.ToInt32(multinum1st);
                                                                            Console.Write("Please enter second number: ");
                                                                            string multinum2st = Console.ReadLine();
                                                                            int multinum2 = Convert.ToInt32(multinum2st);
                                                                            int answer = multinum1 / multinum2;
                                                                            Console.WriteLine("The answer is: " + answer);
                                                                        }
                                                                        catch
                                                                        {
                                                                            Console.WriteLine("ERROR: unable to process(or attemp to divide by zero).");
                                                                            goto divideloop;
                                                                        }
                                                                        Console.Write("Do you wish to continue? (yes/no): ");
                                                                        string mathexit = Console.ReadLine();
                                                                        if (mathexit == "yes")
                                                                        {
                                                                            goto divideloop;
                                                                        }
                                                                        else
                                                                        {
                                                                            goto MathLoop;
                                                                        }
                                                                    }
                                                                    else
                                                                        if (mathcommand == "exit")
                                                                        {
                                                                            goto begin;
                                                                        }
                                                                        else
                                                                            if (mathcommand == "clr")
                                                                            {
                                                                                Console.Clear();
                                                                                goto MathLoop;
                                                                            }
                                                }
                                                else
                                                    if (command != HELP)
                                                    {
                                                        COMMANDERROR(c);
                                                    }
                                                    else
                                                        if (command != DIRECTORY)
                                                        {
                                                        }
                                                        else
                                                            if (command != EDIT)
                                                            {
                                                            }
                                                            else
                                                                if (command != VER)
                                                                {
                                                                }
                                                                else
                                                                    if (command != RESTART)
                                                                    {
                                                                    }
                                                                    else
                                                                        if (command != SHUTOFF)
                                                                        {
                                                                        }
                                                                        else
                                                                            if (command != CLEAR)
                                                                            {
                                                                            }
                                                                            else
                                                                                if (command != RUN)
                                                                                {
                                                                                }
            }
            catch
            {
                Console.WriteLine("ERROR: No such command found!!!!");
                goto begin;
            }
        }

        private static void COMMANDERROR(object c)
        {
            throw new NotImplementedException();
        }
    }
}


